
/****************************************************************************
**
** Copyright (C) 2004-2008 VATATA.com . All rights reserved.
**
** This file is the part of the Miniutil Poject of VATATA.com.
** Vatata's miniutil library is lightweight and portable (for linux
** and Windows). The C++ Class Library included Threadpool, Dll danymic loading, 
** General socket application model and tcp server, httpserver/servlet, etc. 
** It is a pratical utility disigned for who don't want learn the Big C++ 
** Developing Platform, such as  Boost or ACE.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by VATATA.com.
** (or its successors, if any) and the Miniutil Project Aministrators. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://www.gnu.org/licenses/lgpl.html. If you are unsure which 
** license is appropriate for your use, please review the following 
** information: http://code.google.com/p/miniutil/ or contact 
** http://www.vatata.com.
**
** In addition, as a special exception, VATATA.com, as the sole
** copyright holder for Miniutil Project, grants users of VATATA
** P2P Platform the right to release and package with the related 
** libraries without the source code.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. vatata reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/


#ifndef __MINIUTIL__UDP__SOCKET__H__
#define __MINIUTIL__UDP__SOCKET__H__

#include <stdlib.h>
#include "miniutil/socket.h"
#include "miniutil/counter.h"

using namespace std;

namespace miniutil {


	class upacket
	{
	public:
		int port;
		miniutil::ip4addr addr;
		char *data;
		int len;

		inline upacket() 
            : port( 0 )
            , data(NULL)
            , len( 0 )
		{}

        inline upacket(const char * d, int l,const miniutil::soaddr & peeraddr )
            : port( peeraddr.getPort() )
            , addr( peeraddr.getIP() )
            , len( l )
		{
			data = ( char *) malloc( len );
			memcpy(data, d, len );
		}

		inline upacket( const upacket & p )
            : port( p.port )
            , addr( p.addr )
            , len( p.len )
		{
			data = ( char *) malloc( len );
			memcpy(data, p.data, len);
		}

		inline ~upacket()
		{
			if ( data ) delete data;
		}
	};


	class usocket
	{
	protected:
		bool hasBound, hasClosed, hasConnected;
		SOCKET sock;

		miniutil::soaddr bindAddress;
		miniutil::soaddr connectAddress;
		miniutil::soaddr sendtoAddress;
    public:
        MINI_DECLARE_CLASS_COUNTER_VALUE(usocket_send_all_byte)
        MINI_DECLARE_CLASS_COUNTER_VALUE(usocket_recv_all_byte)

	public:
		usocket( const miniutil::soaddr & bindAddr );
		usocket();

		virtual ~usocket();
		virtual void close();

		virtual bool bind( int port, const miniutil::ip4addr & addr = miniutil::ip4addr::INETANY() );
		virtual bool bind( const miniutil::soaddr & bindAddr );

		virtual bool connect(const char * host, int port );
		virtual bool connect(const miniutil::soaddr & cAddr);

		virtual int send( const char * data, int len );
		virtual int sendto( const char * data, int len , miniutil::soaddr & sAddr );

		virtual int recvfrom(char * data, int maxlen,  miniutil::soaddr & fromAddr );

        upacket * recv();

        
        bool setBlockMode( bool isblocked );
		bool waitWriteable(int ms);
		bool waitReadable(int ms);

        inline 
        SOCKET get_sock()
        {
            return sock ;
        }
	};

}


#endif


